/*     */ package com.zimbra.cs.store.triton;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.store.BlobBuilder;
/*     */ import com.zimbra.cs.store.external.ExternalResumableOutputStream;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.security.MessageDigest;
/*     */ import java.util.concurrent.atomic.AtomicLong;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TritonIncomingOutputStream
/*     */   extends ExternalResumableOutputStream
/*     */ {
/*     */   protected final String baseUrl;
/*     */   private final TritonUploadUrl uploadUrl;
/*     */   protected final MessageDigest digest;
/*     */   protected final TritonBlobStoreManager.HashType hashType;
/*     */   protected final MozyServerToken serverToken;
/*     */   protected AtomicLong written;
/*     */   protected ByteArrayOutputStream baos;
/*     */   
/*     */   public TritonIncomingOutputStream(BlobBuilder blobBuilder, MessageDigest digest, TritonBlobStoreManager.HashType hashType, String baseUrl, TritonUploadUrl uploadUrl, MozyServerToken serverToken, AtomicLong written)
/*     */   {
/*  52 */     super(blobBuilder);
/*  53 */     this.baseUrl = baseUrl;
/*  54 */     this.uploadUrl = uploadUrl;
/*  55 */     this.hashType = hashType;
/*  56 */     this.serverToken = serverToken;
/*  57 */     this.digest = digest;
/*  58 */     this.written = written;
/*  59 */     this.baos = new ByteArrayOutputStream(LC.triton_upload_buffer_size.intValue());
/*     */   }
/*     */   
/*     */   protected void writeToExternal(byte[] b, int off, int len) throws IOException
/*     */   {
/*  64 */     this.baos.write(b, off, len);
/*  65 */     if (this.baos.size() >= LC.triton_upload_buffer_size.intValue()) {
/*  66 */       sendHttpData();
/*     */     }
/*     */   }
/*     */   
/*     */   public void flush() throws IOException
/*     */   {
/*  72 */     if (this.baos.size() > 0) {
/*  73 */       sendHttpData();
/*     */     }
/*     */   }
/*     */   
/*     */   public void close() throws IOException
/*     */   {
/*  79 */     super.close();
/*  80 */     if (this.baos.size() > 0) {
/*  81 */       sendHttpData();
/*     */     }
/*     */   }
/*     */   
/*     */   private void sendHttpData() throws IOException {
/*  86 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*     */     
/*  88 */     boolean started = false;
/*  89 */     PostMethod post; PostMethod post; if (this.uploadUrl.isInitialized()) {
/*  90 */       started = true;
/*  91 */       post = new PostMethod(this.baseUrl + this.uploadUrl);
/*     */     } else {
/*  93 */       post = new PostMethod(this.baseUrl + "/blob");
/*     */     }
/*     */     try {
/*  96 */       ZimbraLog.store.info("posting to %s", new Object[] { post.getURI() });
/*  97 */       HttpClientUtil.addInputStreamToHttpMethod(post, new ByteArrayInputStream(this.baos.toByteArray()), this.baos.size(), "application/octet-stream");
/*  98 */       post.addRequestHeader("Content-Length", this.baos.size() + "");
/*  99 */       post.addRequestHeader("X-Mozy-Hash-Type", this.hashType.toString());
/* 100 */       post.addRequestHeader("Content-Range", "bytes " + this.written.longValue() + "-" + (this.written.longValue() + this.baos.size() - 1L) + "/*");
/* 101 */       if (this.serverToken.getToken() != null) {
/* 102 */         post.addRequestHeader("X-Mozy-Server-Token", this.serverToken.getToken());
/*     */       }
/* 104 */       int statusCode = HttpClientUtil.executeMethod(client, post);
/* 105 */       if (statusCode == 200) {
/* 106 */         handleResponse(post);
/* 107 */       } else if ((!started) && (statusCode == 303)) {
/* 108 */         started = true;
/* 109 */         this.uploadUrl.setUploadUrl(post.getResponseHeader("Location").getValue());
/* 110 */         handleResponse(post);
/*     */       } else {
/* 112 */         throw new IOException("Unable to append, bad response code " + statusCode);
/*     */       }
/*     */     } finally {
/* 115 */       post.releaseConnection();
/*     */     }
/* 117 */     this.baos = new ByteArrayOutputStream(LC.triton_upload_buffer_size.intValue());
/*     */   }
/*     */   
/*     */   private void handleResponse(PostMethod post) throws IOException {
/* 121 */     this.serverToken.setToken(post);
/* 122 */     this.written.set(this.written.longValue() + this.baos.size());
/* 123 */     this.digest.update(this.baos.toByteArray());
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/triton/TritonIncomingOutputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */